import { EventEmitter } from "events";

export class Event {
  constructor(
    public type: string,
    public bubbles: boolean = false,
    public cancelable: boolean = false
  ) {}
}

export class DataEvent<T=string> extends Event {
  constructor(
    type: string,
    bubbles: boolean = false,
    cancelable: boolean = false,
    public data?: T
  ) {
    super(type, bubbles, cancelable);
  }
}

export class EventDispatcher<T extends string = string> {
  ee = new EventEmitter();
  addEventListener<T2 extends Event = Event>(
    event: T,
    listener: (e: T2) => void,
    b1?: boolean,
    b2?: boolean,
    b3?: boolean
  ) {
    this.ee.addListener(event, listener);
    return this;
  }
  removeEventListener<T2 extends Event = Event>(
    event: T,
    listener: (e: T2) => void,
    b1?: boolean
  ) {
    this.ee.removeListener(event, listener);
    return this;
  }
  hasEventListener(event: T) {
    return this.ee.eventNames().includes(event);
  }
  dispatchEvent<T2 extends Event = Event>(e: T2) {
    return this.ee.emit(e.type);
  }
}
